import React, { PureComponent, Fragment } from 'react';
import { connect } from 'dva';
import { Button } from 'antd';
import PageHeaderLayout from '../../layouts/PageHeaderLayout';
import ListTable from "./Components/ListTable";
import CloseWarn from "./Components/CloseWarn";


@connect(({ global }) => ({
  dictionaries:global.dictionaries,
  selectSource:global.selectSource,
}))

export default class EquipmentList extends PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      modalWarnCloseVisible: false,
      modalWarnCloseinfo:null,
      detailId: 0,
      detailInfoId: 0,
      detailInfoName:null,
      isReloadList:false,
    };
  }

  componentWillUnmount(){
    const { dispatch } = this.props;
    dispatch({
      type: `warn/appendClearState`,
    })
  }

  componentDidMount() {
    const { dispatch } = this.props;
    // 数据字典
    // dispatch({
    //   type: 'global/fetch_dictionar',
    //   // payload:["devType", "status", "whitelistReason"]
    // });

  }

  //
  sotpReloadList = () =>{
    this.setState({
      isReloadList:false,
    });
  }

  // 关闭告警信息
  handleCloseWarn = (row) => {
    this.setState({
      modalWarnCloseinfo: row,
    },()=>{
      this.handleCloseWarnModalVisible(true);
    });
  };

  // 显示关闭告警信息model
  handleCloseWarnModalVisible = flag => {
    this.setState({
      modalWarnCloseVisible: !!flag,
    });
  };

  // 填写关闭告警原因后回调
  handleCloseWarnModelCallBack=(value,reinfo)=>{
    this.handleCloseWarnModalVisible(false);
    const { dispatch } = this.props;
    const { modalWarnCloseinfo } = this.state;

    if(value && modalWarnCloseinfo){
      dispatch({
        type: 'warn/action_warn_close',
        payload: {
          warnId:modalWarnCloseinfo.warnId,
          ...value,
        },
      }).then(()=>{
        if(modalWarnCloseinfo.from=="info"){
          dispatch({
            type: "warn/fetch_warn_info",
            payload: {
              devId: modalWarnCloseinfo.devId
            }
          });
        }
        this.setState({
          modalWarnCloseinfo: null,
          isReloadList:true,
        });

        // this.getDataList(formValues,pageSize,currentPage);
      });
    }

  }


  // 展示详情
  handleWarnInfo= (id,name) => {
    this.setState(
      {
        detailInfoId: id,
        detailInfoName:name
      }
    );
  };




  render() {
    const { dictionaries, selectSource, location} = this.props;
    const { modalWarnCloseVisible, isReloadList, modalWarnCloseinfo } = this.state;

    return (
      <PageHeaderLayout >
        <ListTable
          dictionaries={dictionaries}
          selectSource={selectSource}
          detailModelCallBack={this.handleWarnInfo}
          closeWarnCallBack={this.handleCloseWarn}
          sotpReloadList={this.sotpReloadList}
          isReloadList={isReloadList}
          location={location}
        />

        <CloseWarn
          visible={modalWarnCloseVisible}
          onCancel={e => this.handleCloseWarnModalVisible(e)}
          callback={this.handleCloseWarnModelCallBack}
          warninfo={modalWarnCloseinfo}
          dictionaries={dictionaries}
        />

      </PageHeaderLayout>

    );
  }
}
